﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileSC.Configs
{
    public partial class AppConfig
    {
        /// <summary>
        /// 主配置文件
        /// </summary>
        public static ExcelConfig ExcelConfig
        {
            get { return new ExcelConfig("app"); }
        }
        /// <summary>
        /// 读取全局配置值
        /// </summary>
        public static T ReadGobalValue<T>(string key)
        {
            return ExcelConfig.ReadValue<T>("全局", key);
        }

        public static void WriteGlobalValue(string key, string value)
        {
            ExcelConfig.WriteGlobal(key, value);
        }
        /// <summary>
        /// 读取全局文本
        /// </summary>
        public static string Text(string key)
        {
            return ReadGobalValue<string>(key);
        }
        /// <summary>
        /// 写入全局文本
        /// </summary>
        public static void WriteText(string key, string value)
        {
            WriteGlobalValue(key, value);
        }
        public static void Save()
        {
            ExcelConfig.Save();
        }
        /// <summary>
        /// 读取全局数组
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string[] Array(string key)
        {
            return Text(key).Split(';').Where(t => !string.IsNullOrEmpty(t)).ToArray();
        }
        /// <summary>
        /// 读取全局开关值
        /// </summary>
        public static bool YesOrNo(string key)
        {
            return ReadGobalValue<string>(key) == "是的";
        }
        /// <summary>
        /// 写入全局开关值
        /// </summary>
        public static void WriteYesOrNo(string key, bool value)
        {
            WriteGlobalValue(key, value?"是的": "不是");
        }
        /// <summary>
        /// 获得额外配置
        /// </summary>
        public static ExcelConfig GetExtraExcelConfig(string name)
        {
            var _conf = new ExcelConfig(ReadExcelConfigName(name));
            _conf.SetPath(ReadGobalValue<string>("额外配置目录"));
            return _conf;
        }
        /// <summary>
        /// 读取额外配置文件名
        /// </summary>
        public static string ReadExcelConfigName(string key)
        {
            return ExcelConfig.ReadValue<string>("额外配置", key);
        }
        /// <summary>
        /// 读取额外的配置值
        /// </summary>
        public static T ReadExtraExcelConfigValue<T>(string name, string sheetname, string key)
        {
            return GetExtraExcelConfig(ReadExcelConfigName(name)).ReadValue<T>(sheetname, key);
        }
    }
}
